package primaryAlgorithm;

import org.junit.Test;

import java.util.Arrays;

//https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/
//LC删除排序数组中的重复项
public class Code01 {
    /*
    0 <= nums.length <= 3 * 104
    -104 <= nums[i] <= 104
    nums 已按升序排列

     */
    public int removeDuplicates(int[] nums) {// 使用双指针解决这个问题1
        // 排除异常情况
        if (nums == null || nums.length == 0){
            return 0;
        }
        int left = 0;
        for (int right = 1;right<nums.length;right++){// 右指针一直移动，当右指针的值与左指针不同的时候。交换位置
            if (nums[left] != nums[right])
                nums[++left] = nums[right];
        }
        return ++left;
    }

    @Test
    public void test(){
        int[] nums = {1,1, 2, 3, 4};
        System.out.println("removeDuplicates(new int[]{1,1,1,2,3,4}) = " + removeDuplicates(nums));
        System.out.println(Arrays.toString(nums));
    }
}
